package com.open.jdbc.pool;

import java.sql.*;
import java.util.concurrent.ConcurrentLinkedQueue;

public class ConnectionPoolTest {

    public static void main(String[] args) throws SQLException {
        ConnectionPool connectionPool = new ConnectionPool();
        for (int i = 0; i < 20; i++) {
            new Thread(()->{
                Connection connection = null;
                try {
                    //获取连接
                    connection = connectionPool.getConnection();
                    PreparedStatement preparedStatement = connection.prepareStatement("select * from testtable limit 1");
                    ResultSet resultSet = preparedStatement.executeQuery();

                    while (resultSet.next()) { // 遍历每一行结果
                        int id = resultSet.getInt("id"); // 通过列名获取数据
                        System.out.println(Thread.currentThread().getName()+" 数据："+id);
                    }

                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }finally {
                    try {
                        //归还连接
                        connectionPool.recycleConnection(connection);
                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    }
                }
            }).start();
        }
    }

}
